package com.ouken.asynTask;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import com.gen.tool.AlertDialog_PG;
import com.gen.tool.AlertDialog_ok;
import com.gen.tool.SoundPlayer;
import com.ouken.com.HPActivity;
import com.ouken.com.LoginActivity;
import com.ouken.com.R;
import com.ouken.com.RoomlistActivity;
import com.ouken.tool.ConnectServer;
import com.ouken.tool.MyApp;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AsynLogin extends AsyncTask<String, TextView, String> {

	private Context context;
	private EditText et_email ;
	private EditText et_passwd;
	private WifiManager wifi;
	private List params;
	private String httpUrl;
	private MyApp appState;
	//private ProgressDialog dialog;
	private SharedPreferences userInfo;
	
	private AlertDialog_PG PGDialog;
	private AlertDialog PGnow;

	public AsynLogin(Context context, EditText et_email , EditText et_passwd,  MyApp appState) {
		super();
		this.context = context;
		this.et_email = et_email;
		this.et_passwd = et_passwd;
		this.wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 
		this.params = new ArrayList();
		this.httpUrl = "http://"+ConnectServer.getServerAdd()+"/login";
		this.appState = appState;
		//this.dialog = new ProgressDialog(context);
		this.userInfo = context.getSharedPreferences("user_info", 0);
		
		PGDialog = new AlertDialog_PG(context,"Please wait \nwhile loading...");
	}
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		
		System.out.println("AsynLogin onpreexcute");

		final AlertDialog PGnow1 = PGDialog.AlertDialog_PG_show();
		PGnow = PGnow1;
        /*dialog.setMessage("Please wait while loading...");
        dialog.setIndeterminate(true);
        dialog.setCancelable(true);
        dialog.show();*/
        
		params.add(new BasicNameValuePair("email", et_email.getText().toString()));
		params.add(new BasicNameValuePair("passwd", et_passwd.getText().toString()));
		params.add(new BasicNameValuePair("mac", wifi.getConnectionInfo().getMacAddress()));
		params.add(new BasicNameValuePair("ParamEnd", "ParamEndParamEndParamEnd"));
		
	}
	@Override
	protected String  doInBackground(String...strings ) {
		//Toast.makeText(context, "doInBackground", Toast.LENGTH_SHORT).show();
		System.out.println("AsynLogin doInBackground");
		//String httpUrl ="http://36.55.231.19:8090/login";
		return ConnectServer.connect(httpUrl,params);
		
	}

	@Override
    protected void onPostExecute(String result) {
		//Toast.makeText(context, "onPostExecute", Toast.LENGTH_SHORT).show();
		System.out.println("AsynLogin onPostExecute");
		
		final AlertDialog PGnow_c = PGnow;
		
		if(result == null){
			PGnow_c.dismiss();
			 //dialog.dismiss();
			 AlertDialog alertDialog =
					new AlertDialog.Builder(context)
	                .setIcon(R.drawable.alert_dialog_icon)
	                .setTitle("ERROR!")
	                .setMessage(R.string.ERROR_NETWORKOFF)
	                .setPositiveButton("OK",new DialogInterface.OnClickListener() {
	                    public void onClick(DialogInterface dialog, int whichButton) {
	                    	SoundPlayer.playSound(R.raw.keypress_common);
	                    	userInfo.edit().putString("AUTOLOGIN", "NO").commit(); 
	                    	Intent intent = new Intent(context, LoginActivity.class);
	            			context.startActivity(intent);
	            			((Activity)context).finish();
	                    }
	                 })
	                .create();
				
			alertDialog.show();
			
			return;
		}
		String[] resultParams =result .split(",");
		
		if(resultParams[0].toString().equals("OK")){
			
			appState.setUserEmail(et_email.getText().toString());
			appState.setPasswd(et_passwd.getText().toString());
			appState.setMac(wifi.getConnectionInfo().getMacAddress());
			
			userInfo.edit().putString("AUTOLOGIN", "YES").commit(); 
			userInfo.edit().putString("EMAIL", appState.getUserEmail()).commit(); 
			userInfo.edit().putString("PWD", appState.getPasswd()).commit(); 
			
			//new AysnGetAccount(appState, context, dialog).execute();
			PGnow_c.dismiss();
			new AysnGetAccount(appState,context).execute();
		}else{
			PGnow_c.dismiss();
			//dialog.dismiss();
			
			AlertDialog_ok myDialog = new AlertDialog_ok(context,resultParams[1]);
        	final AlertDialog now = myDialog.AlertDialog_ok_show();
		    
        	myDialog.getConfirm_btn_ok().setOnClickListener(new View.OnClickListener(){
				@Override  
			    public void onClick(View v) {  
					SoundPlayer.playSound(R.raw.keypress_common);
					userInfo.edit().putString("AUTOLOGIN", "NO").commit(); 
                	Intent intent = new Intent(context, LoginActivity.class);
                	intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        			context.startActivity(intent);
        			((Activity)context).finish();
					} 
				}
			);  	
			
		}
		
    }
	

	

	
	
	
}
